//
//  OUserInfo.m
//  OFootball
//
//  Created by 上官千城 on 15/9/22.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OUserInfo.h"
#import "AppDelegate.h"
#import "ODbManager.h"

@implementation OUserInfo

+(OUserInfo*) loadInfo
{
    
    ODbManager* dbManager = [ODbManager getDomainManager:USER_INFO];
    OUserInfo* info = (OUserInfo*)[dbManager getObject:@"user_self"];
    return info;
}

+(OUserInfo*) getInfo
{
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    return appDelegate.userinfo;
}

+(void) setInfo:(OUserInfo *)info
{
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    appDelegate.userinfo = info;
}

+(BOOL) deleteInfo
{
    ODbManager* dbmanager = [ODbManager getDomainManager:USER_INFO];
    BOOL succ = [dbmanager deleteObject:@"user_self"];
    if (succ) {
        [OUserInfo setInfo:nil];
    }
    return succ;
}

-(void)update
{
    ODbManager* dbmanager = [ODbManager getDomainManager:USER_INFO];
    [dbmanager setObject:self forKey:@"user_self"];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeInteger:self.number forKey:@"number"];
    [aCoder encodeObject:self.userName forKey:@"userName"];
    [aCoder encodeObject:self.telNumber forKey:@"telNumber"];
    [aCoder encodeObject:self.token forKey:@"token"];
    [aCoder encodeObject:self.sign forKey:@"sign"];
    [aCoder encodeInteger:self.userId forKey:@"userId"];
    [aCoder encodeBool:self.sex forKey:@"sex"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        self.number = [aDecoder decodeIntegerForKey:@"number"];
        self.userName = [aDecoder decodeObjectForKey:@"userName"];
        self.telNumber = [aDecoder decodeObjectForKey:@"telNumber"];
        self.token = [aDecoder decodeObjectForKey:@"token"];
        self.sign = [aDecoder decodeObjectForKey:@"sign"];
        
        self.userId = [aDecoder decodeIntegerForKey:@"userId"];
        self.sex = [aDecoder decodeBoolForKey:@"sex"];
        
        return self;
    }
    return nil;
}

@end

